public class Solution999 {
    public int numRookCaptures(char[][] board) {
        for (int i=0;i<board.length;i++){
            for (int j=0;j<board[0].length;j++){
                if (board[i][j]=='R'){
                    int ans=0;
                    int t=i-1;
                    while (t>=0 && board[t][j]=='.'){
                        t--;
                    }
                    if (t>=0 && board[t][j]=='p'){
                        ans++;
                    }
                    t=i+1;
                    while (t<board.length && board[t][j]=='.'){
                        t++;
                    }
                    if (t<board.length && board[t][j]=='p'){
                        ans++;
                    }
                    t=j-1;
                    while (t>=0 && board[i][t]=='.'){
                        t--;
                    }
                    if (t>=0 && board[i][t]=='p'){
                        ans++;
                    }
                    t=j+1;
                    while (t<board[0].length && board[i][t]=='.'){
                        t++;
                    }
                    if (t<board[0].length && board[i][t]=='p'){
                        ans++;
                    }
                    return ans;
                }
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(new Solution999().numRookCaptures(new char[][]{{'.','.','.','.','.','.','.','.'},
                {'.','.','.','p','.','.','.','.'},{'.','.','.','R','.','.','.','p'},{'.','.','.','.','.','.','.','.'},
                {'.','.','.','.','.','.','.','.'},{'.','.','.','p','.','.','.','.'},{'.','.','.','.','.','.','.','.'},
                {'.','.','.','.','.','.','.','.'}}));
    }
}
